package com.zs.infrastructure.util.invoker.tushare;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zs.infrastructure.util.constant.TuShareConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @author tushare接口调用工具类
 * @date 2022/8/28 0028 下午 8:43
 * @desc
 **/
@Slf4j
@Component
public class TuShareInvoker {

    @Value("${hf.stock.tuShare.path}")
    private String path;
    @Value("${hf.stock.tuShare.token}")
    private String token;

    @Autowired
    private RestTemplate restTemplate;

    public <T> TuShareInvokerReponse<T> sendPost(TuShareInvokerRequest tuShareInvokerRequest,Class<T> responseClass) {
        TuShareInvokerReponse result = new TuShareInvokerReponse();
        try {
            HttpHeaders httpHeaders = new HttpHeaders();
            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("token",token);
            paramMap.put("api_name",tuShareInvokerRequest.getRequestPath());
            paramMap.put("fields",tuShareInvokerRequest.getFields());
            Map<String,Object> requestParamMap = (Map) JSONObject.parseObject(JSON.toJSON(JSON.toJSONString(tuShareInvokerRequest)).toString());
            if(null != requestParamMap && requestParamMap.size() > 0) {
                paramMap.put("params",requestParamMap);
            }

            //设置请求参数
//            MultiValueMap<String, Object> requestMap = new LinkedMultiValueMap<>();
//            paramMap.forEach(requestMap::add);
            //设置参数
            HttpEntity<Map> requestEntity = new HttpEntity<>(paramMap, httpHeaders);
            String url = path;
            log.info("tushare接口入参tuShareInvokerRequest:{}", JSON.toJSONString(tuShareInvokerRequest));
            ResponseEntity<TuShareInvokerReponse> responseEntity = restTemplate.postForEntity(url, requestEntity, TuShareInvokerReponse.class);
            log.info("tushare出参responseEntity:{}", JSON.toJSONString(responseEntity));

//            if(null != responseEntity
//                    && null != responseEntity.getBody()
//                    && null != responseEntity.getBody().getCode()
//                    && responseEntity.getBody().getCode().equals("0")) {
//                String responseString = JSON.toJSONString(responseEntity.getBody().getData());
//                result = JSON.parseObject(responseString, TuShareInvokerReponse.class);
//            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info("tushare接口请求失败:{}", e.getMessage());
        }
        return result;
    }

}
